import ctypes
import tkinter
from MoveWindow import get_rect_no_shadow
from PySpyCore import (
    get_cursor_pos,
    get_hwnd_from_pos,
    get_title,
    get_title_p,
    get_title_pp,
)
from ResourcePath import get_assets_dir


class TkWindow:
    def __init__(self):
        self.tk_init()

    def update_info(self):
        def get_info():
            p = get_cursor_pos()
            hwnd = get_hwnd_from_pos(p)
            title_c = get_title(hwnd)
            title_p = get_title_p(hwnd)
            title_pp = get_title_pp(hwnd)
            # print("坐标 " + ",".join("%s" % id for id in p))
            # print("句柄 " + "%d" % hwnd)
            # print("标题 " + title_c)
            # print("标题 " + ",".join("%s" % id for id in title_p))
            return p, title_c, title_p, title_pp

        li = get_info()
        self.listb.delete(0, tkinter.END)
        for item in li:
            self.listb.insert(tkinter.END, item)

        if hasattr(self, "xentry"):
            rect_noshadow = get_rect_no_shadow(li[-1][0])
            x = rect_noshadow.left
            y = rect_noshadow.top
            self.xentry.delete(0, "end")
            self.yentry.delete(0, "end")
            self.xentry.insert(0, x)
            self.yentry.insert(0, y)

    def tk_mainloop(self):
        self.tk.mainloop()

    def get_screen_size(self):
        return self.tk.winfo_screenwidth(), self.tk.winfo_screenheight()

    def test(self, content, widget_name):
        # "0-"          允许零后负号，并自动更正为负号
        if content == "0-":
            if "entry2" in widget_name:
                self.yentry.delete(0, "end")
                self.yentry.insert(0, "-")
            else:
                self.xentry.delete(0, "end")
                self.xentry.insert(0, "-")
        # lstrip/"-"    允许负数
        # ""            允许删光，更符合人类操作
        if content.lstrip("-").isdigit() or content == "-" or content == "":
            return True
        else:
            return False

    def tk_init(self):
        self.tk = tkinter.Tk()
        test_cmd = self.tk.register(self.test)  # 通过register方法转换为validatecommand选项能接收的函数
        self.tk.geometry("500x300")
        self.tk.resizable(0, 0)
        self.tk.title("窗口移动工具")
        self.tk.iconbitmap(get_assets_dir("logo.ico"))

        # 禁止缩放
        # ctypes.windll.user32.SetProcessDPIAware()
        # 告知操作系统，程序将自行适配dpi
        # 同时影响获取其他窗口时的结果,GetWindowRect,DwmGetWindowAttribute(DMWA_EXTENDED_FRAME_BOUNDS)结果会很诡异
        ctypes.windll.shcore.SetProcessDpiAwareness(1)
        # 获取屏幕的缩放因子
        ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
        # 设置程序缩放
        self.tk.tk.call("tk", "scaling", ScaleFactor / 75)

        self.img1 = tkinter.PhotoImage(file=get_assets_dir("target_1.png"))
        self.img2 = tkinter.PhotoImage(file=get_assets_dir("target_2.png"))
        self.imgLabel = tkinter.Label(self.tk, image=self.img1, bg="Dodgerblue")
        self.imgLabel.pack(fill=tkinter.Y, side=tkinter.LEFT)

        frame1 = tkinter.Frame(self.tk, bg="DeepSkyBlue")
        frame1.pack(fill=tkinter.X, side=tkinter.TOP)

        listb_t = tkinter.Listbox(
            frame1,
            width=4,
            height=0,
            bg="DeepSkyBlue",
            highlightbackground="DeepSkyBlue",
            highlightcolor="DeepSkyBlue",
            relief=tkinter.FLAT,
        )
        listb_t.insert(tkinter.END, "坐标")
        listb_t.insert(tkinter.END, "标题")
        listb_t.insert(tkinter.END, "标题")
        listb_t.insert(tkinter.END, "标题")
        listb_t.width = 9
        listb_t.pack(side=tkinter.LEFT)
        self.listb = tkinter.Listbox(
            frame1,
            height=0,
            bg="DeepSkyBlue",
            highlightbackground="DeepSkyBlue",
            highlightcolor="DeepSkyBlue",
            relief=tkinter.FLAT,
        )
        self.update_info()
        self.listb.pack(fill=tkinter.X)

        frame2 = tkinter.Frame(self.tk, bg="LightSkyBlue")
        frame2.pack(fill=tkinter.BOTH, expand="yes")
        frame2_ = tkinter.Frame(frame2, bg="Blue")
        frame2_.pack(expand="yes")

        frame21 = tkinter.Frame(frame2_, bg="LightSkyBlue")
        frame21.pack(fill=tkinter.BOTH, expand="yes")
        frame22 = tkinter.Frame(frame2_, bg="LightSkyBlue")
        frame22.pack(fill=tkinter.BOTH, expand="yes")

        x = tkinter.Label(frame21, text="X", width=16, bg="LightSkyBlue")
        x.pack(side=tkinter.LEFT)
        y = tkinter.Label(frame21, text="Y", width=16, bg="LightSkyBlue")
        y.pack(side=tkinter.LEFT)
        self.xentry = tkinter.Entry(
            frame22,
            width=16,
            textvariable=tkinter.StringVar(value="0"),
            validate="key",
            validatecommand=(test_cmd, "%P", "%W"),
        )
        self.xentry.pack(side=tkinter.LEFT)
        self.yentry = tkinter.Entry(
            frame22,
            width=16,
            textvariable=tkinter.StringVar(value="0"),
            validate="key",
            validatecommand=(test_cmd, "%P", "%W"),
        )
        self.yentry.pack(side=tkinter.LEFT)

        frame3 = tkinter.Frame(self.tk, bg="DeepSkyBlue")
        frame3.pack(fill=tkinter.BOTH, expand="yes")
        frame3_ = tkinter.Frame(frame3, bg="DeepSkyBlue")
        frame3_.pack(expand="yes")

        self.useroption = tkinter.StringVar()
        self.useroption.set("右上角")
        optionmenu = tkinter.OptionMenu(
            frame3_, self.useroption, "左上角", "右上角", "左下角", "右下角", "自定义"
        )
        optionmenu.pack(side=tkinter.LEFT)
        self.button = tkinter.Button(
            frame3_,
            text="      移动      ",
            image=tkinter.PhotoImage(width=1, height=1),
            compound="c",
            height=25,
        )
        self.button.pack(side=tkinter.LEFT)
